﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HSDc.RingleFlow.Kernel.DBPersit.Dao
{
    internal class RepositoryServiceDao
    {
        internal ProcessDefinitionDto GetProcessDefinition(string definitionId)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var definition = from pd in entity.ProcessDefinitionDto
                             where (pd.Definition_GUID.ToLower() == definitionId.ToLower())
                             select pd;
            return definition.First();
        }

        internal ProcessDefinitionDto GetProcessDefinitionByName(string definitionName)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var definition = from pd in entity.ProcessDefinitionDto
                             where ((pd.Definition_Name == definitionName) &&
                                        (pd.Definition_ACTIVE))
                             orderby pd.Definition_Version descending
                             select pd;
            return definition.First();
        }

        internal int GetActiveProcessInstance(int definitionId)
        {
            return 0;
        }

        internal List<ProcessActivityDto> GetDefaultActivities(int definitionId)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var acts = from pa in entity.ProcessActivityDto
                             where (pa.Activity_Belong_Definition==definitionId)
                             orderby pa.Activity_ID
                             select pa;
            return acts.ToList();
        }

        internal List<TransitionDto> GetTransitionsBySource(int sourceActId)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var trans = from pa in entity.TransitionDto
                       where (pa.Transition_Source == sourceActId)
                       orderby pa.Transition_ID
                       select pa;
            return trans.ToList();
        }

        internal SwimlaneDto GetActivityBelongSwimlane(int activityId)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var activity = (from pa in entity.ProcessActivityDto
                         where pa.Activity_ID == activityId
                         select pa).Single();
            var swimlane = (from sl in entity.SwimlaneDto
                            where ((sl.Swimlane_Definition_ID == activity.Activity_Belong_Definition) &&
                                       (sl.Swimlane_SEQ == activity.Actvity_Swimlane))
                            select sl).Single();
            return swimlane;
        }

        internal int GetOrgType(string swimlaneGuid)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            int oType;
            var orgType = from or in entity.OrganizationDto
                          join sl in entity.SwimlaneDto on or.Org_ID equals sl.Swimlane_Org_ID
                          where sl.Swimlane_GUID == swimlaneGuid
                          select or.Org_TYPE;
            if (orgType.ToList().Count == 0) oType = 0;
            else oType = orgType.First();
            return oType;
        }
    }
}
